<template>
    <div class="app-container">
        <div style="text-align: center">{{createStatus?'添加':'更新'}}工单信息</div>
        <div style="width: 50%;margin:0px auto;vertical-align:middle;height:100%">
            <el-form :model="workOrderFrom" ref="workOrderFrom" :rules="workOrderFromRules" label-width="100px">
                <el-form-item label="业主企业" prop="enterpriseId" >
                    <el-select v-model="workOrderFrom.enterpriseId" placeholder="请选择所属企业" style="width: 100%" v-if="enterPriseHidden">
                        <el-option v-for="(item,index) in enterpriseList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="工单名称" prop="name">
                    <el-input v-model="workOrderFrom.name" placeholder="请输入名称" style="width: 100%"></el-input>
                </el-form-item>
                <el-form-item label="地点" prop="address">
                    <el-input v-model="workOrderFrom.address" placeholder="请输入地点" style="width: 100%"></el-input>
                </el-form-item>
<!--                <el-form-item label="异常图片" prop="abnormalPic">-->
<!--                    <el-input v-model="workOrderFrom.abnormalPic" placeholder="请输入异常图片" style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="隐患描述" prop="remark">
                    <el-input v-model="workOrderFrom.remark" placeholder="请输入隐患描述" style="width: 100%"></el-input>
                </el-form-item>
<!--                <el-form-item label="区域Id" prop="regionId">-->
<!--                    <el-input v-model="workOrderFrom.regionId" placeholder="请输入维保详情" style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="执行人" prop="executePerson">-->
<!--                    <el-input v-model="workOrderFrom.executePerson" placeholder="请输入执行人" style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="执行时间" prop="executeTime">-->
<!--                    <el-input v-model="workOrderFrom.executeTime" placeholder="请输入执行时间" style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="完成时间" prop="completeTime">-->
<!--                    <el-input v-model="workOrderFrom.completeTime" placeholder="请输入完成时间" style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="反馈时间" prop="feedbackTime">-->
<!--                    <el-input v-model="workOrderFrom.feedbackTime" placeholder="请输入反馈时间" style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="反馈描述" prop="feedbackRemark">-->
<!--                    <el-input v-model="workOrderFrom.feedbackRemark" placeholder="请输入反馈描述"-->
<!--                              style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="审核时间" prop="examineTime">-->
<!--                    <el-input v-model="workOrderFrom.examineTime" placeholder="请输入审核时间" style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="审核意见" prop="examineOpinion">-->
<!--                    <el-input v-model="workOrderFrom.examineOpinion" placeholder="请输入审核意见"-->
<!--                              style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="审核状态" prop="examineStatus">-->
<!--                    <el-input v-model="workOrderFrom.examineStatus" placeholder="请输入审核状态"-->
<!--                              style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="工单状态" prop="status">-->
<!--                    <el-input v-model="workOrderFrom.status" placeholder="请输入工单状态" style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
<!--                <el-form-item label="完成图片" prop="completeUrl">-->
<!--                    <el-input v-model="workOrderFrom.completeUrl" placeholder="请输入完成图片" style="width: 100%"></el-input>-->
<!--                </el-form-item>-->
            </el-form>
        </div>
        <div slot="footer" style="text-align: center" class="dialog-footer">
            <el-button @click="cancelCommission">取消</el-button>
            <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="addInWorkOrder">创 建
            </el-button>
            <el-button :loading="addLoading" v-else type="primary" @click="updateInWorkOrder">更新</el-button>
        </div>
    </div>
</template>

<script>

import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {getInUserList} from "@/api/enterprise";
import {addInWorkOrder, getInWorkOrderVo, updateInWorkOrder} from "@/api/workOrder";
export default {
    name: "edit",
    data(){
        return{
            workOrderFrom:{

            },
            workOrderFromRules:{
                // designee: [{ required: true, message: '请选择指定人', trigger: 'blur' }],
                enterpriseId: [{ required: true, message: '请选择企业', trigger: 'blur' }],
                // equipmentId: [{ required: true, message: '请选择设备', trigger: 'blur' }],
            },
            addLoading:false,
            enterpriseList:[],
            executorList:[]
        }
    },
    computed: {
        createStatus(){
            return this.$route.query.ucId==undefined
        },
      enterPriseHidden(){
        let roleCode = this.$store.getters.roleCode;
        return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"||roleCode=="WB-ADMIN"
      }
    },
    created() {
        if(!this.createStatus){
            let ucId =this.$route.query.ucId
            console.log(ucId)
            this.getInWorkOrderVo(ucId)
        }
        this.getInEnterpriseDominantList();
        this.getInUserList();
    },
    methods:{
        cancelCommission(){
            window.history.back()
        },
        getInWorkOrderVo(val) {
            getInWorkOrderVo({ucId: val}).then(res =>{
                console.log(res)
                this.workOrderFrom = res.data
            }).catch(res => {
                this.addLoading = false
                this.$notify.error({
                    title: '失败',
                    message: res.msg
                })
            })
        },
        addInWorkOrder() {
            this.addLoading = true
            this.$refs['workOrderFrom'].validate(valid => {
                if (valid) {
                    addInWorkOrder(this.workOrderFrom).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加保单成功"
                        })
                        this.cancelCommission()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")


                }
            })
        },
        updateInWorkOrder() {
            this.addLoading = true
            this.$refs['workOrderFrom'].validate(valid => {
                if (valid) {
                    updateInWorkOrder(this.workOrderFrom).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新保单成功"
                        })
                        this.cancelCommission()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        getInEnterpriseDominantList() {
            getInEnterpriseDominantList({}).then(res => {
                this.enterpriseList=res.data;
            }).catch(res => {
                this.addLoading = false
                this.$notify.error({
                    title: '失败',
                    message: res.msg
                })
            })
        },
        getInUserList() {
            getInUserList({}).then(res => {
                this.executorList = res.data
                console.log(res.data)
            }).catch(res => {
                this.addLoading = false
                this.$notify.error({
                    title: '失败',
                    message: res.msg
                })
            })
        },

    }

}
</script>

<style
        scoped>

</style>